package com.uc.sdk.g3;

import com.uc.sdk.g3.vo.FileVo;
import com.uc.sdk.g3.vo.G3Content;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.reflect.MethodUtils;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

public class G3FileUtil {
    /**
     * 根据文件名处理
     * @param files
     * @param bytes
     * @return
     */
    public static G3Content make(List<FileVo> files, byte[] bytes){
        int total = bytes.length;
        G3Content g3Content = new G3Content();
        //逆序处理字节长度
        for (int i = files.size() - 1; i >=0; i--) {
            FileVo file = files.get(i);
            byte[] self = ArrayUtils.subarray(bytes,total - file.getSize(),total);
            total = total - file.getSize();
            file.setData(self);
            if(file.getFileName().equals(".xml")){
                try {
                    g3Content.setXml(new String(self,"gb2312"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }else{
                String methodName = file.getFileName();
                switch (methodName) {
                    case "big.jpg":
                        g3Content.setBigJpg(file.getData());
                        break;
                    case "small.jpg":
                        g3Content.setSmallJpg(file.getData());
                        break;
                    case ".bin":
                        g3Content.setBin(file.getData());
                        break;
                    case "img2.jpg":
                        g3Content.setImg2Jpg(file.getData());
                        break;
                    case "img3.jpg":
                        g3Content.setImg3Jpg(file.getData());
                        break;
                    case "img4.jpg":
                        g3Content.setImg4Jpg(file.getData());
                        break;
                }
            }
//            MethodUtils.invokeExactMethod()
        }
        return g3Content;
    }
}
